package com.sunwayworld.baseline.framework.utils;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class MemoryMonitoringUtils {
	
	private static final Logger logger = LogManager.getLogger(MemoryMonitoringUtils.class);
	
	public static boolean checkMemoryUsage(double momeryThreshold) {
		
        Runtime run = Runtime.getRuntime();
        
        long max = run.maxMemory(); // 最大内存
        
        long total = run.totalMemory(); // 已分配内存
        
        long free = run.freeMemory(); // 已分配内存中剩余空间
        
        double ratio = (((double)(total - free)) / max) * 100;
        logger.info("系统当前最大内存:{}，已分配内存：{}，已分配内存中剩余空间：{}", max, total, free);
        logger.info("系统当前已使用内存：{}%，设置内存阀值：{}%", ratio, momeryThreshold);
        if (ratio  > momeryThreshold) {
        	return false;
        }
        
        return true;
    }

}
